define(['app'], function( app ) {
    'use strict';

    app.directive('contenteditable',['crmConst',function( crmConst ) {
            return {
                restrict: 'A' ,
                require: '?ngModel',
                link: function(scope, element, attrs, ngModel) {
                    if (!ngModel) {
                        return;
                    }
                    ngModel.$render = function() {
                        element.html(ngModel.$viewValue || '');
                    };
                    element.on('blur keyup change', function() {
                        scope.$apply(readViewText);
                    });

                    wangEditor.config.printLog = false;
                    // 创建编辑器
                    var editor = new wangEditor(element);

                    function readViewText() {
                        var html = element.html();
                        if (attrs.stripBr && html === '<br>') {
                            html = '';
                        }
                        ngModel.$setViewValue(html);
                    }


                    editor.config.uploadImgUrl = crmConst.BASE + crmConst.API + 'Att/upload';
                    editor.config.hideLinkImg = true;
                    editor.config.uploadImgFileName = 'file';
                    editor.config.uploadParams = {
                        from: 'editor'
                    };
                    editor.create();
                }
            };
        }]);
});
